/*!
 * 文件名称： main.c
 * 描    述： CAN-CAN通信实验主函数文件
 * 版本：     2023-12-03, V1.0
*/

/*
* GD32F303东方红开发板V1.0
* 淘宝商城：   https://juwo.taobao.com
* 技术交流：   https://www.gd32bbs.com/ask/
* 视频学习：   https://space.bilibili.com/475462605
* 微信公众号： gd32bbs
* Copyright    苏州聚沃电子科技有限公司
* 版权所有，盗版必究。
*/

#include "gd32f30x.h"
#include <stdio.h>
#include "bsp_can.h"
#include "driver_public.h"
#include "bsp_uart.h"

/*!
* 说明     main函数
* 输入     无
* 输出     无
* 返回值   无
*/
int main(void)
{
	driver_init();//delay函数初始化
	bsp_uart_init(&BOARD_UART);//BOARD_UART串口初始化
	bsp_can_config(BSP_CAN);//BOARD_CAN初始化
	nvic_irq_enable(USBD_LP_CAN0_RX0_IRQn,0,0);//使能CAN0 FIFO0 NVIC
	nvic_irq_enable(CAN0_RX1_IRQn,0,0);//使能CAN0 FIFO1 NVIC
	while (1)
	{
		bsp_can_transmit(BSP_CAN,&bsp_can_transmit_message_1);//发送一帧数据
		printf_log("\r\n can0 transmit data:%x,%x", bsp_can_transmit_message_1.tx_data[0], bsp_can_transmit_message_1.tx_data[1]);//发送数据打印
		delay_ms(1000); //延时1s
		bsp_can_transmit(BSP_CAN,&bsp_can_transmit_message_2);//发送一帧数据
		printf_log("\r\n can0 transmit data:%x,%x", bsp_can_transmit_message_2.tx_data[0], bsp_can_transmit_message_2.tx_data[1]);//发送数据打印
		delay_ms(1000);
		bsp_can_transmit(BSP_CAN,&bsp_can_transmit_message_3);//发送一帧数据
		printf_log("\r\n can0 transmit data:%x,%x", bsp_can_transmit_message_3.tx_data[0], bsp_can_transmit_message_3.tx_data[1]);//发送数据打印
		delay_ms(1000);		
		if(can0_receive_fifo0_flag == SET)
		{
			printf_log("\r\n can0_fifo0 receive ID = %x data:%x,%x", can0_receive_message_fifo0.rx_sfid,can0_receive_message_fifo0.rx_data[0], can0_receive_message_fifo0.rx_data[1]);//接收数据打印
			can0_receive_fifo0_flag = RESET; //标志位清除
		}
		if(can0_receive_fifo1_flag == SET)
		{
			printf_log("\r\n can0_fifo1 receive ID = %x data:%x,%x", can0_receive_message_fifo1.rx_sfid,can0_receive_message_fifo1.rx_data[0], can0_receive_message_fifo1.rx_data[1]);//接收数据打印
			can0_receive_fifo1_flag = RESET;
		}
	}
}
